package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.School;
import com.wuseguang.springcloud.service.SchoolService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @描述 数据管理-->学校信息
 * @创建人 zm
 * @创建时间 2019/11/25
 */
@RestController
@RequestMapping("/school")
public class SchoolController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SchoolService schoolService;


    /**
     * 增加
     *
     * @param school
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody School school) {
        try {
            schoolService.add(school);
            return new Result(CodeUtil.SUCCESS);
        } catch (Exception e) {
            logger.info("error={}", e);
            return new Result(CodeUtil.ADD_FAILURE);
        }
    }

    /**
     * 修改
     *
     * @param school
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody School school) {
        try {
            schoolService.update(school);
            return new Result(CodeUtil.SUCCESS);
        } catch (Exception e) {
            logger.info("error={}", e);
            return new Result(CodeUtil.MODIFICATION_FAILED);
        }
    }

    /**
     * @描述 删除
     * @创建人 zm
     * @创建时间 2019/11/26
     */
    @RequestMapping("/delete")
    public Result delete(int id) {
        try {
            schoolService.delete(id);
            return new Result(CodeUtil.SUCCESS);
        } catch (Exception e) {
            logger.info("error={}", e);
            return new Result(CodeUtil.DELETE_FAILED);
        }
    }

    /**
     * 获取实体
     * @param
     * @return
     */
    @RequestMapping("/findOne")
    public Result findOne(RequestParameterWrapper request) {
        String id = request.getParam("schoolId");
        logger.info("schoolId={}",id);
        if (id!=null){
            return  new Result(schoolService.findOne(Integer.valueOf(id)));
        }
        return new Result(CodeUtil.QUERY_FAILED);
    }
}
